package com.st.emps.department.service;

import java.util.ArrayList;
import java.util.List;

import com.st.emps.department.dao.DepartmentDao;
import com.st.emps.department.model.DepartmentModel;

public class DepartmentService {

	private DepartmentDao dao = new DepartmentDao();
	
	public String insert(DepartmentModel model) {
		
		return selectModel(model)==null? dao.insert(model):"repeat";

	}
	public String delete(DepartmentModel model) {
		long countS = dao.select(model).get(0).getCountS();
		if(countS==0) {
			return dao.delete(model);
		}
		return 2+"";
	}
	public String update(DepartmentModel model) {
		return dao.update(model);

	}
	public List<DepartmentModel> selectList(DepartmentModel model) {
		String code = model.getCode();
		if(code==null)
			code ="";
		model.setCode("%"+code+"%");
		String name = model.getName();
		if(name==null)
			name ="";
		model.setName("%"+name+"%");
		return dao.select(model);

	}
	public DepartmentModel selectModel(DepartmentModel model) {
		DepartmentModel tmp = new DepartmentModel();
		List<DepartmentModel> list = new ArrayList<DepartmentModel>();
		tmp.setCode(model.getCode());
		list = dao.select(tmp);
		if(list!=null&&!list.isEmpty())
		return list.get(0);
		return null;

	}
	public Object selectCount(DepartmentModel model) {
		DepartmentModel tmp = new DepartmentModel();
		String code = model.getCode();
		if(code==null)
			code ="";
		tmp.setCode("%"+code+"%");
		String name = model.getName();
		if(name==null)
			name ="";
		tmp.setName("%"+name+"%");
		return dao.selectCount(tmp);
	}
}
